package com.vm.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;

import com.vm.adapter.FragAdapter;
import com.vm.myview.NoScrollViewPager;
import com.wwtscapp.apk.R;

@SuppressLint("InflateParams")
public class TopFragment extends BaseFragment {

	Button rbtn_month_ranking;
	Button rbtn_week_ranking;
	Button rbtn_day_ranking;
	TabHost tabHost;
	Button btn_search;
	ImageView imgbtn_back;
	private View view;
	private NoScrollViewPager vp;
	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	List<Fragment> fragments = new ArrayList<Fragment>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("CategoryFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.ranking, null);
			
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		isPrepared = true;
		lazyLoad();

	}

	public void init() {
		rbtn_month_ranking = (Button) view
				.findViewById(R.id.rbtn_month_ranking);
		rbtn_week_ranking = (Button) view.findViewById(R.id.rbtn_week_ranking);
		rbtn_day_ranking = (Button) view.findViewById(R.id.rbtn_day_ranking);
		// btn_search = (Button) view.findViewById(R.id.btn_search);
		// imgbtn_back = (ImageButton) view.findViewById(R.id.imgbtn_back);
		rbtn_month_ranking.setOnClickListener(btn_listener);
		rbtn_week_ranking.setOnClickListener(btn_listener);
		rbtn_day_ranking.setOnClickListener(btn_listener);
		vp = (NoScrollViewPager) view.findViewById(R.id.viewpager);
		// btn_search.setOnClickListener(searchClickListener);
		// imgbtn_back.setOnClickListener(btn_listener);
	}

	public OnClickListener btn_listener = new OnClickListener() {

		public void onClick(View v) {
			if (v == rbtn_day_ranking) {
				vp.setCurrentItem(0);
			}
			if (v == rbtn_week_ranking) {
				vp.setCurrentItem(1);
			} else if (v == rbtn_month_ranking) {
				vp.setCurrentItem(2);
			}
		}

	};

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				// UIHelper.showDialogForLoading(getActivity(), "正在加载...",
				// true);
				mainHandler.sendMessage(new Message());
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);

					// 在这里添加调用接口获取数据的代码
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功

					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				// UIHelper.hideDialogForLoading();
			}
		}.execute();
	}

	private void iniChilds() {
		try {
			
			if(fragments.size()<1){
				TopDayFragment topDayFragment = new TopDayFragment();
				TopWeekFragment topWeekFragment = new TopWeekFragment();
				TopMonthFragment topMonthFragment = new TopMonthFragment();
				fragments.add(topDayFragment);
				fragments.add(topWeekFragment);
				fragments.add(topMonthFragment);

				FragAdapter adapter = new FragAdapter(getChildFragmentManager(),
						fragments);
				vp.setAdapter(adapter);

				// 设置最大缓存数
				// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
				vp.setOffscreenPageLimit(3);
				vp.setCurrentItem(0);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressLint("HandlerLeak")
	private Handler mainHandler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			init();
			iniChilds();
		};
	};
}
